<?php
$config = require 'config.php';
$nick = $_GET['user'] ?? '';
$code = $_GET['code'] ?? '';
if (!$nick || !$code) exit('参数缺失');

$dsn = "mysql:host={$config['db']['host']};dbname={$config['db']['dbname']};charset={$config['db']['charset']}";
$pdo = new PDO($dsn, $config['db']['user'], $config['db']['pass'], [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$stmt = $pdo->prepare(
    "SELECT id, created_at FROM users
     WHERE nickname = ? AND verify_code = ? AND activated_at IS NULL"
);
$stmt->execute([$nick, $code]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$user) exit('无效链接或已激活');

// 检查1小时内
if (time() - strtotime($user['created_at']) > 3600) {
    // 删除记录
    $pdo->prepare("DELETE FROM users WHERE id = ?")->execute([$user['id']]);
    exit('激活超时，请重新注册');
}

// 激活账号
$activatedAt = date('Y-m-d H:i:s');
$pdo->prepare(
    "UPDATE users SET activated_at = ? WHERE id = ?"
)->execute([$activatedAt, $user['id']]);

echo '激活成功，欢迎！';
?>